﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UNSWThink
{
   /// <summary>
   /// Windows form that allows users to enter a password
   /// </summary>
   public partial class ClassAccountPasswordForm : Form
   {
      /// <summary>
      /// Default constructor
      /// </summary>
      public ClassAccountPasswordForm()
      {
         InitializeComponent();
      }

      #region CONTROL EVENTS

      private void buttonCancel_Click(object sender, EventArgs e)
      {
         this.Close();
      }

      private void textBoxPassword_TextChanged(object sender, EventArgs e)
      {
         if (textBoxPassword.Text == String.Empty)
         {
            textBoxRetypePassword.Enabled = false;
            buttonSetPassword.Enabled = false;
         }
         else
         {
            textBoxRetypePassword.Enabled = true;

            if (textBoxRetypePassword.Text != String.Empty)
            {
               buttonSetPassword.Enabled = true;
            }
         }
      }

      private void buttonSetPassword_Click(object sender, EventArgs e)
      {
         if (CheckCompleted())
         {
            if (textBoxPassword.Text == textBoxRetypePassword.Text)
            {
               string hashedPassword = HelperMethods.GetMd5Hash(textBoxPassword.Text);
               AddEditClassForm aecf = (AddEditClassForm)this.Owner;
               aecf.Password = hashedPassword;

               this.Close();
            }
            else
            {
               MessageBox.Show("The passwords do not match", "Incorrect passwords",
                  MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
         }
      }

      #endregion

      #region HELPER METHODS

      private bool CheckCompleted()
      {
         if ((textBoxPassword.Text != String.Empty) &&
             (textBoxRetypePassword.Text != String.Empty))
         {
            return true;
         }

         return false;
      }

      #endregion

      private void textBoxRetypePassword_TextChanged(object sender, EventArgs e)
      {
         if (textBoxRetypePassword.Text == String.Empty)
         {
            buttonSetPassword.Enabled = false;
         }
         else
         {
            buttonSetPassword.Enabled = true;
         }
      }
   }
}
